package cn.stylefeng.guns.modular.gridsystem.enums;

import lombok.Getter;

/**
 * FailureLevelEnum 故障等级
 */
@Getter
public enum FailureLevelEnum {

    LOW(0, "low"),

    /**
     * 该条记录不存在
     */
    MIDDLE(1, "middle"),

    /**
     * 编码重复
     */
    HEIGHT(2, "height");

    private final Integer code;

    private final String name;

    FailureLevelEnum(int code, String name) {
        this.code = code;
        this.name = name;
    }

    /**
     * 根据code查找
     *
     * @param code 枚举code
     * @return 枚举对象
     */
    public static String findEnumByCode(Integer code) {
        for (FailureLevelEnum levelEnum : FailureLevelEnum.values()) {
            if (levelEnum.getCode().equals(code)) {
                return levelEnum.name;
            }
        }
        throw new IllegalArgumentException("code is invalid");
    }

    /**
     * 根据name查找
     *
     * @param name 枚举name
     * @return 枚举对象
     */
    public static Integer findEnumByName(String name) {
        for (FailureLevelEnum levelEnum : FailureLevelEnum.values()) {
            if (levelEnum.getName().equals(name)) {
                return levelEnum.code;
            }
        }
        throw new IllegalArgumentException("name is invalid");
    }


}
